package pl.prv.polanie.server.service;

import pl.prv.polanie.client.dto.AcademicTitleDTO;
import pl.prv.polanie.client.dto.BuildingDTO;
import pl.prv.polanie.client.dto.CSAcademicTitleDTO;
import pl.prv.polanie.client.dto.CSBuildingDTO;
import pl.prv.polanie.client.dto.CSCalendarDayDTO;
import pl.prv.polanie.client.dto.CSDeclarationDTO;
import pl.prv.polanie.client.dto.CSGroupDTO;
import pl.prv.polanie.client.dto.CSLecturerDTO;
import pl.prv.polanie.client.dto.CSPreferenceDTO;
import pl.prv.polanie.client.dto.CSPreferenceTypeDTO;
import pl.prv.polanie.client.dto.CSRoomDTO;
import pl.prv.polanie.client.dto.CSRoomTypeDTO;
import pl.prv.polanie.client.dto.CSSemesterDTO;
import pl.prv.polanie.client.dto.CSSemesterTypeDTO;
import pl.prv.polanie.client.dto.CSSemestersYearGroupsDTO;
import pl.prv.polanie.client.dto.CSStudiesTypeDTO;
import pl.prv.polanie.client.dto.CSSubgroupDTO;
import pl.prv.polanie.client.dto.CSSubgroupTypeDTO;
import pl.prv.polanie.client.dto.CSSubjectDTO;
import pl.prv.polanie.client.dto.CSSubjectEventDTO;
import pl.prv.polanie.client.dto.CSSubjectNameDTO;
import pl.prv.polanie.client.dto.CSSubjectTypeDTO;
import pl.prv.polanie.client.dto.CSYearGroupNameDTO;
import pl.prv.polanie.client.dto.CalendarDayDTO;
import pl.prv.polanie.client.dto.DeclarationDTO;
import pl.prv.polanie.client.dto.GroupDTO;
import pl.prv.polanie.client.dto.LecturerDTO;
import pl.prv.polanie.client.dto.PreferenceDTO;
import pl.prv.polanie.client.dto.PreferenceTypeDTO;
import pl.prv.polanie.client.dto.RoomDTO;
import pl.prv.polanie.client.dto.RoomTypeDTO;
import pl.prv.polanie.client.dto.SemesterDTO;
import pl.prv.polanie.client.dto.SemesterTypeDTO;
import pl.prv.polanie.client.dto.SemestersYearGroupsDTO;
import pl.prv.polanie.client.dto.StudiesTypeDTO;
import pl.prv.polanie.client.dto.SubgroupDTO;
import pl.prv.polanie.client.dto.SubgroupTypeDTO;
import pl.prv.polanie.client.dto.SubjectDTO;
import pl.prv.polanie.client.dto.SubjectEventDTO;
import pl.prv.polanie.client.dto.SubjectNameDTO;
import pl.prv.polanie.client.dto.SubjectTypeDTO;
import pl.prv.polanie.client.dto.YearGroupNameDTO;

public class PlannerServiceUtil {

	public static CSPreferenceDTO[] convertFromPreferenceDTOToCSPreferenceDTO(PreferenceDTO[] dtos) {
		CSPreferenceDTO[] result = new CSPreferenceDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			PreferenceDTO dto = dtos[i];
			
			CSPreferenceDTO csDto = new CSPreferenceDTO(dto.getId(), dto.getPreferenceType().getId(),
					dto.getLecturer().getId(), dto.getCalendarDay().getId(), dto.getStartTime(), dto.getDuration());
			
			result[i] = csDto;
		}
		return result;
	}

	public static CSSubgroupDTO[] convertFromSubgroupDTOToCSSubgroupDTO(SubgroupDTO[] dtos) {
		CSSubgroupDTO[] result = new CSSubgroupDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			SubgroupDTO dto = dtos[i];
			
			CSSubgroupDTO csDto = new CSSubgroupDTO(dto.getId(), dto.getSubgroupType().getId(), dto.getGroup().getId(),
					dto.getName(), dto.getStudentsNumber());
			
			result[i] = csDto;
		}
		return result;
	}
		
	public static CSSubjectEventDTO[] convertFromSubjectEventDTOToCSSubjectEventDTO(SubjectEventDTO[] dtos) {
		CSSubjectEventDTO[] result = new CSSubjectEventDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			SubjectEventDTO dto = dtos[i];
			
	
//			long subjectID = -1;
//			if (dto.getSubject() != null) {
//				subjectID = dto.getSubject().getId();
//			}
			long subjectID = dto.getSubject().getId();
			
			
			long roomID = -1;
			if (dto.getRoom() != null) {
				roomID = dto.getRoom().getId();
			}
			
//			long subgroupID = -1;
//			if (dto.getSubgroup() != null) {
//				subgroupID = dto.getSubgroup().getId();
//			}
			long subgroupID = dto.getSubgroup().getId();
			
			long lecturerID = -1;
			if (dto.getLecturer() != null) {
				lecturerID = dto.getLecturer().getId();
			}
			
			long calendarDayID = -1;
			if (dto.getCalendarDay() != null) {
				calendarDayID = dto.getCalendarDay().getId();
			}
			
			long groupID = dto.getSubgroup().getGroup().getId();
			long semesterID = dto.getSubgroup().getGroup().getSemestersYearGroup().getSemester().getId();
			
			
			CSSubjectEventDTO csDto = new CSSubjectEventDTO(dto.getId(), dto.getEventSort(),
					subjectID, roomID, subgroupID,
					lecturerID, calendarDayID,
					groupID, semesterID,
					dto.getStartTime(), dto.getDuration());
			
			result[i] = csDto;
		}
		return result;
	}
	
	
	public static CSDeclarationDTO[] convertFromDeclarationDTOToCSDeclarationDTO(DeclarationDTO[] dtos) {
		CSDeclarationDTO[] result = new CSDeclarationDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			DeclarationDTO dto = dtos[i];
			
			long groupID = dto.getGroup().getId();
			
			long subgroupTypeID = dto.getSubgroupType().getId();
			
			long subjectID = dto.getSubject().getId();
			
			CSDeclarationDTO csDto = new CSDeclarationDTO(dto.getId(), groupID, subgroupTypeID,
					subjectID, dto.getDuration(), new Integer(0));
//			CSDeclarationDTO csDto = new CSDeclarationDTO(dto.getId(), groupID, subgroupTypeID,
//					subjectID, dto.getSubgroupsAmount(), dto.getDuration(), new Integer(0));
			
			result[i] = csDto;
		}
		return result;
	}
	
	
	public static CSCalendarDayDTO[] convertFromCalendarDayDTOToCSCalendarDayDTO(CalendarDayDTO[] dtos) {
		CSCalendarDayDTO[] result = new CSCalendarDayDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			CalendarDayDTO dto = dtos[i];
			
			long semesterID = -1;
			if (dto.getSemester() != null) {
				semesterID = dto.getSemester().getId();
			}
			
			CSCalendarDayDTO csDto = new CSCalendarDayDTO(dto.getId(), dto.getSemester().getId(),
					dto.getDayLabel(), dto.getDisplayOrder(), dto.isSunday());
			
			result[i] = csDto;
		}
		return result;
	}
	
	public static CSGroupDTO[] convertFromGroupDTOToCSGroupDTO(GroupDTO[] dtos) {
		CSGroupDTO[] result = new CSGroupDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			GroupDTO dto = dtos[i];
			
			
			long semestersYearGroup = -1;
			if (dto.getSemestersYearGroup() != null) {
				semestersYearGroup = dto.getSemestersYearGroup().getId();
			}
			
			
			
			CSGroupDTO csDto = new CSGroupDTO(dto.getId(), semestersYearGroup,
					dto.getName(), dto.getStudentsNumber(), dto.getScheduleProgress());
			
			result[i] = csDto;
		}
		
		return result;
	}
	
	public static CSLecturerDTO[] convertFromLecturerDTOToCSLecturerDTO(LecturerDTO[] dtos) {
		CSLecturerDTO[] result = new CSLecturerDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			LecturerDTO dto = dtos[i];

			CSLecturerDTO csDto = new CSLecturerDTO(dto.getId(), dto.getName(), dto.getSurname(), dto.getAcademicTitle().getId());

			result[i] = csDto;
		}
		return result;
	}
	
	public static CSAcademicTitleDTO[] convertFromAcademicTitleDTOToCSAcademicTitleDTO(AcademicTitleDTO[] dtos) {
		CSAcademicTitleDTO[] result = new CSAcademicTitleDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			AcademicTitleDTO dto = dtos[i];

			CSAcademicTitleDTO csDto = new CSAcademicTitleDTO(dto.getId(), dto.getTitle());

			result[i] = csDto;
		}
		return result;
	}


	public static CSRoomDTO[] convertFromRoomDTOToCSRoomDTO(RoomDTO[] dtos) {
		CSRoomDTO[] result = new CSRoomDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			RoomDTO dto = dtos[i];

			CSRoomDTO csDto = new CSRoomDTO(dto.getId(), dto.getName(), dto.getCapacity(), dto.getBuilding().getId(),
					dto.getRoomType().getId());

			result[i] = csDto;
		}
		return result;
	}
	
	
	public static CSSubjectDTO[] convertFromSubjectDTOToCSSubjectDTO(SubjectDTO[] dtos) {
		CSSubjectDTO[] result = new CSSubjectDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			SubjectDTO dto = dtos[i];

			CSSubjectDTO csDto = new CSSubjectDTO(dto.getId(), dto.getSubjectType().getId(), dto.getSubjectName().getId());

			result[i] = csDto;
		}
		return result;
	}
	
	
	public static CSSemestersYearGroupsDTO[] convertFromSemestersYearGroupsDTOToCSSemestersYearGroupsDTO(SemestersYearGroupsDTO[] dtos) {
		CSSemestersYearGroupsDTO[] result = new CSSemestersYearGroupsDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			SemestersYearGroupsDTO dto = dtos[i];
			
			CSSemestersYearGroupsDTO csDto = new CSSemestersYearGroupsDTO(dto.getId(), dto.getSemester().getId(),
					dto.getYearGroupName().getId(), dto.getStudentsNumber());
			result[i] = csDto;
		}
		return result;
	}

	
	public static CSSemesterDTO[] convertFromSemesterDTOToCSSemesterDTO(SemesterDTO[] dtos) {
		CSSemesterDTO[] result = new CSSemesterDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			
			SemesterDTO dto = dtos[i];
			CSSemesterDTO csDto = new CSSemesterDTO(dto.getId(), dto.getStudiesType().getId(),
					dto.getSemesterType().getId(), dto.getYearNumber(), dto.isVisibleForOrdinaryUsers());
			
			result[i] = csDto;
		}
		return result;
	}

	public static CSStudiesTypeDTO[] convertFromStudiesTypeDTOToCSStudiesTypeDTO(StudiesTypeDTO[] dtos) {
		CSStudiesTypeDTO[] result = new CSStudiesTypeDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			StudiesTypeDTO dto = dtos[i];
			CSStudiesTypeDTO csDto = new CSStudiesTypeDTO(dto.getId(), dto.getName());

			result[i] = csDto;
		}
		return result;
	}

	public static CSSemesterTypeDTO[] convertFromSemesterTypeDTOToCSSemesterTypeDTO(SemesterTypeDTO[] dtos) {
		CSSemesterTypeDTO[] result = new CSSemesterTypeDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			SemesterTypeDTO dto = dtos[i];
			CSSemesterTypeDTO csDto = new CSSemesterTypeDTO(dto.getId(), dto.getName());

			result[i] = csDto;
		}
		return result;
	}

//	public static CSYearNumberDTO[] convertFromYearNumberDTOToCSYearNumberDTO(YearNumberDTO[] dtos) {
//		CSYearNumberDTO[] result = new CSYearNumberDTO[dtos.length];
//		
//		for (int i = 0; i < dtos.length; i++) {
//			YearNumberDTO dto = dtos[i];
//			CSYearNumberDTO csDto = new CSYearNumberDTO(dto.getId(), dto.getNumber());
//
//			result[i] = csDto;
//		}
//		return result;
//	}

	public static CSSubgroupTypeDTO[] convertFromSubgroupTypeDTOToCSSubgroupTypeDTO(SubgroupTypeDTO[] dtos) {
		CSSubgroupTypeDTO[] result = new CSSubgroupTypeDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			SubgroupTypeDTO dto = dtos[i];
			CSSubgroupTypeDTO csDto = new CSSubgroupTypeDTO(dto.getId(), dto.getName());

			result[i] = csDto;
		}
		return result;
	}
	
	
	public static CSSubjectNameDTO[] convertFromSubjectNameDTOToCSSubjectNameDTO(SubjectNameDTO[] dtos) {
		CSSubjectNameDTO[] result = new CSSubjectNameDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			SubjectNameDTO dto = dtos[i];

			CSSubjectNameDTO csDto = new CSSubjectNameDTO(dto.getId(), dto.getName(), dto.getSymbol());

			result[i] = csDto;
		}
		return result;
	}


	public static CSSubjectTypeDTO[] convertFromSubjectTypeDTOToCSSubjecttypeDTO(SubjectTypeDTO[] dtos) {
		CSSubjectTypeDTO[] result = new CSSubjectTypeDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			SubjectTypeDTO dto = dtos[i];

			CSSubjectTypeDTO csDto = new CSSubjectTypeDTO(dto.getId(), dto.getName(), dto.getInitial());

			result[i] = csDto;
		}
		return result;
	}
	
	
	public static CSBuildingDTO[] convertFromBuildingDTOToCSBuildingDTO(BuildingDTO[] dtos) {
		CSBuildingDTO[] result = new CSBuildingDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			BuildingDTO dto = dtos[i];
			CSBuildingDTO csDto = new CSBuildingDTO(dto.getId(), dto.getName(), dto.getSymbol());
			result[i] = csDto;
		}
		return result;
	}


	public static CSRoomTypeDTO[] convertFromRoomTypeDTOToCSRoomDTO(RoomTypeDTO[] dtos) {
		CSRoomTypeDTO[] result = new CSRoomTypeDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			RoomTypeDTO dto = dtos[i];

			CSRoomTypeDTO csDto = new CSRoomTypeDTO(dto.getId(), dto.getName());

			result[i] = csDto;
		}
		return result;
	}
	
	public static CSYearGroupNameDTO[] convertFromYearGroupNameDTOToCSYearGroupNameDTO(YearGroupNameDTO[] dtos) {
		CSYearGroupNameDTO[] result = new CSYearGroupNameDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			YearGroupNameDTO dto = dtos[i];

			CSYearGroupNameDTO csDto = new CSYearGroupNameDTO(dto.getId(), dto.getName());

			result[i] = csDto;
		}
		return result;
	}

	public static CSPreferenceTypeDTO[] convertFromPreferenceTypeDTOToCSPreferenceTypeDTO(PreferenceTypeDTO[] dtos) {
		CSPreferenceTypeDTO[] result = new CSPreferenceTypeDTO[dtos.length];
		
		for (int i = 0; i < dtos.length; i++) {
			PreferenceTypeDTO dto = dtos[i];

			CSPreferenceTypeDTO csDto = new CSPreferenceTypeDTO(dto.getId(), dto.getName(), dto.getColor());

			result[i] = csDto;
		}
		return result;
	}

	
	
	
}
